package zhcms.cms;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import zhcms.db.DBManager;
import zhcms.db.DbConn;
import zhcms.db.ConnectionPool.PooledConnection;
import zhcms.tool.GyPage;

public class GyArticleList {
	
	String pageStr="";
	String ColumnName="";
	int pSize=15;
	int pagenum;


	public GyArticleList()
	{
		
	}

	public int listcount(){
		ResultSet rs = null;
		PooledConnection conn = null;
		String sql="select count(*) as id from `gyhy_content`";
		int countlyb=0;
		try {
			conn = DBManager.getConnection();
			rs = conn.executeQuery(sql);
			if (rs.next())
			{
				countlyb=rs.getInt("id");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
            conn.close();
		}
		return countlyb;
	}
	
	public int listcount(String listid){
		ResultSet rs = null;
		PooledConnection conn = null;
		String sql="select count(*) as id from `gyhy_content` where ColumnId='"+listid+"'";
		int countlyb=0;
		try {
			conn = DBManager.getConnection();
			rs = conn.executeQuery(sql);
			if (rs.next())
			{
				countlyb=rs.getInt("id");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
            conn.close();
		}
		return countlyb;
	}
	
	public ArrayList<HashMap<String, String>> getAllList(int currentpage)
	{
		int listnumcount=this.listcount();
		GyPage gyp=new GyPage();
		gyp.setP(pSize, listnumcount, "", "", currentpage);
		String limitStr=gyp.limit();
		pageStr=gyp.outPut();
		setPagenum(gyp.getPageNum());
		String sql="select * from `gyhy_content` order by id desc limit "+limitStr;
		
		return DbConn.executeQuery(sql);
	}
	
	public String getColumnName()
	{
		return this.ColumnName;
	}
	
	public void setColumnInfo(String listid)
	{
		String sql="select * from `gyhy_column` where Id='"+listid+"'";
		ArrayList<HashMap<String, String>> gytS;
		gytS=new ArrayList<HashMap<String, String>>();
		gytS=DbConn.executeQuery(sql);
		for(int i=0;i<gytS.size();i++)
		{
			Map<String,String> iS=new HashMap<String,String>();
			iS =gytS.get(i);
			this.ColumnName=iS.get("Name");
		}
	}
	
	public ArrayList<HashMap<String, String>> getAllList(int currentpage,String listid)
	{
		int listnumcount=this.listcount(listid);
		//System.out.println(listnumcount);
		GyPage gyp=new GyPage();
		gyp.setP(pSize, listnumcount, "", "listid="+listid+"", currentpage);
		String limitStr=gyp.limit();
		pageStr=gyp.outPut();
		setPagenum(gyp.getPageNum());
		String sql="select * from `gyhy_content` where ColumnId='"+listid+"' order by id desc limit "+limitStr;
		//System.out.println(sql);
		return DbConn.executeQuery(sql);
	}
	
	public String getPageStr() {
		return pageStr;
	}

	public void setPageStr(String pageStr) {
		this.pageStr = pageStr;
	}
	
	
	public int getPagenum() {
		return pagenum;
	}

	public void setPagenum(int pagenum) {
		this.pagenum = pagenum;
	}

}
